<?php
/**
 * ClassName: ScheduleService
 * Description:
 * @Author 周昌
 * @Create 2023/12/4 10:34
 */

namespace service\admin;


use Exception;
use libs\Tool;
use model\AdminModel;
use model\ScheduleModel;
use service\BaseService;
use think\Db;

class ScheduleService extends BaseService
{
    protected static $model = 'model\ScheduleModel';
    static public $today = [0 => '上午', 1 => '下午'];
    static public $week = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];

    //编辑问诊排班数据
    public static function addEdit($week, $today, $doctor_ids)
    {
        $del_where = [
            'week' => $week,
            'today' => $today,
            'hospital_id' => self::getUserHospitalId()
        ];
        if(empty(current($doctor_ids))){
            $del_res = self::$model::delInfo($del_where);
            if ($del_res === false) {
                return Tool::Error();
            } else {
                return Tool::Success();
            }
        }
        $doctor = AdminModel::getListColumn([['id', 'in', $doctor_ids]], 'id,group_name');
        $data = [];
        foreach ($doctor_ids as $k => $v) {
            $row = [
                'admin_id' => $v,
                'doctor_name' => $doctor[$v],
                'addtime' => time(),
            ];
            $data[] = array_merge($row, $del_where);
        }
        Db::startTrans();
        try {
            $del_res = self::$model::delInfo($del_where);
            $insert_res = self::$model::insertMore($data);
            if ($del_res === false || !$insert_res) {
                Db::rollback();
                return Tool::Error();
            } else {
                Db::commit();
                return Tool::Success();
            }
        } catch (Exception $exception) {
            Db::rollback();
            return Tool::Error($exception->getMessage());
        }
    }

    //获取排班表格指定单元格医生数据
    public static function getScheduleDoctor($hospital_id, $week, $today)
    {
        $doctors = AdminModel::getList(['hospital_id' => $hospital_id, 'role' => RoleService::$role['doctor']['id']], 'id,group_name as doctor_name');
        $list = ScheduleModel::getList(['hospital_id' => $hospital_id, 'today' => $today, 'week' => $week]);
        $admin_ids = $list ? array_column($list, 'admin_id') : [];
        foreach ($doctors as $k => $v) {
            $doctors[$k]['checked'] = '';
            if (in_array($v['id'], $admin_ids)) {
                $doctors[$k]['checked'] = 'checked';
            }
        }
        return $doctors;
    }
    //获取当前时间的排班医生
    public static function getCurrentDoctorSelect()
    {
        $hospital_id=self::getUserHospitalId();
        //am or pm
        if (date('A') == 'AM') {
            $today=0;
        } else {
            $today=1;
        }
        //week
        $week=date('w');
        $where=[
            'hospital_id'=>$hospital_id,
            'today'=>$today,
            'week'=>$week,
        ];
        $list=self::$model::getList($where,'admin_id as id,doctor_name as name');
        $schedule_doctor_select = [];
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $row = [];
                $row['id'] = $v['id'];
                $row['name'] = $v['name'];
                $schedule_doctor_select[] = $row;
            }
            array_unshift($schedule_doctor_select, ['id' => '', 'name' => '-----请选择-----']);
        }
        return $schedule_doctor_select;

    }

    /**
     * #User: Administrator
     * #Time: 2023/12/5 17:06
     * #Author:周昌
     * #Description: 获取问诊医生排班数据
     * @param $hospital_id 医院id
     * @param $admin_id 医生id，可选，某医生问诊排班
     * @return \string[][]
     */
    public static function getAjaxData($hospital_id,$admin_id=null)
    {
        $where=['hospital_id' => $hospital_id];
        if($admin_id){
            $where['admin_id']=$admin_id;
        }
        $week = [
            'sun' => ['id' => 0],
            'mon' => ['id' => 1],
            'tue' => ['id' => 2],
            'wed' => ['id' => 3],
            'thu' => ['id' => 4],
            'fri' => ['id' => 5],
            'sat' => ['id' => 6],
        ];
        $data = [
            ['today' => '0'],
            ['today' => '1'],
        ];
        foreach ($data as $k => $v) {
            $data[$k] = array_merge($v, $week);
        }
        $list = self::$model::getList($where);
        if ($list) {
            foreach ($list as $k => $v) {
                if ($v['today'] == 0) {
                    switch ($v['week']) {
                        case 0:
                            $data[0]['sun']['doctor_name'][] = $v['doctor_name'];
                            break;
                        case 1:
                            $data[0]['mon']['doctor_name'][] = $v['doctor_name'];
                            break;
                        case 2:
                            $data[0]['tue']['doctor_name'][] = $v['doctor_name'];
                            break;
                        case 3:
                            $data[0]['wed']['doctor_name'][] = $v['doctor_name'];
                            break;
                        case 4:
                            $data[0]['thu']['doctor_name'][] = $v['doctor_name'];
                            break;
                        case 5:
                            $data[0]['fri']['doctor_name'][] = $v['doctor_name'];
                            break;
                        case 6:
                            $data[0]['sat']['doctor_name'][] = $v['doctor_name'];
                            break;
                    }
                } elseif ($v['today'] == 1) {
                    switch ($v['week']) {
                        case 0:
                            $data[1]['sun']['doctor_name'][] = $v['doctor_name'];
                            break;
                        case 1:
                            $data[1]['mon']['doctor_name'][] = $v['doctor_name'];
                            break;
                        case 2:
                            $data[1]['tue']['doctor_name'][] = $v['doctor_name'];
                            break;
                        case 3:
                            $data[1]['wed']['doctor_name'][] = $v['doctor_name'];
                            break;
                        case 4:
                            $data[1]['thu']['doctor_name'][] = $v['doctor_name'];
                            break;
                        case 5:
                            $data[1]['fri']['doctor_name'][] = $v['doctor_name'];
                            break;
                        case 6:
                            $data[1]['sat']['doctor_name'][] = $v['doctor_name'];
                            break;
                    }
                }

            }
        }

        return $data;
    }

}